<?php
namespace app\api\controller;

use app\BaseController;
use app\common\Sms;
use app\model\CompanyBalance;
use app\model\LeasingCompany as SelfModel;
use app\model\Admin;

use think\facade\Cache;
use think\facade\Cookie;
use think\Response;
use think\Request;
class LeasingCompany extends Base
{


    public function initialize()
    {
        parent::initialize();
        $this->model = "\app\model\LeasingCompany";
        $this->list_rows = 10;
        $this->index = ['id','title','name','email','city','login_account','adminer_id','create_time'];
        $this->all = ['id','title','name','email','city','year_fee','login_account','login_password','adminer_id','create_time'];
        $this->my = ['id','title','name','email','city','year_fee','login_account','login_password','adminer_id','create_time'];
        $this->view = ['id','title','name','mobile','email','city','year_fee','login_account','login_password','adminer_id','create_time'];
        $this->msg = '租赁公司';
        
        
    }

    public function my(Request $request)
    {
        $data = $this->model::field(['id', 'logo', 'title', 'balance', 'login_account'])->find($request->id);
//        $data['year_fee'] = \app\model\DeviceRent::where('company_id', $request->id)->sum('price');
        $data['year_fee'] = $data['balance'];
        $data['balance'] = \app\model\DeviceRent::where('company_id', $request->id)->sum('price');
        return ['code' => 200, 'msg' => '我的', 'data' => $data];
    }

    public function update_password(Request $request)
    {
        if ($request->login_password != $request->confirm_password) {
            return ['code' => 400, 'msg' => '2次密码不一样'];
        }
        $key = 'captcha_update_password_'.$request->mobile;
        $code = Cache::get('captcha_update_password_' . $request->mobile);
        if (Cache::get('captcha_update_password_' . $request->mobile) != $request->captcha) {
            return ['code' => 400, 'msg' => '验证码不对'];
        };
        $ret = $this->model::where('id', $request->id)->update(['login_password' => $request->login_password]);
        if ($ret !== false) {
            return ['code' => 200, 'msg' => '密码修改成功'];
        }
        return ['code' => 400, 'msg' => '失败'];
    }

    public function update_mobile(Request $request)
    {


        //var_export(Cache::get('captcha_update_mobile_' . $request->old_login_account));
        if (Cache::get('captcha_update_mobile_' . $request->old_login_account) != $request->captcha) {
            return ['code' => 400, 'msg' => '验证码不对'];
        };
        $ret = $this->model::where('id', $request->id)->update(['login_account' => $request->login_account]);
        if ($ret !== false) {
            return ['code' => 200, 'msg' => '密码修改成功'];
        }
        return ['code' => 400, 'msg' => '失败'];
    }


    /**
     * 验证码
     */
    public function captcha(Request $request)
    {
        $mobile = $request->login_account;
        $type = $request->type;
        try {
            if (empty($mobile)) {
                return ['code' => 400, 'msg' => '请填写手机号'];
            }
            $find = $this->model::where('login_account', $mobile)->find();
            if ($type == 'register' && !empty($find)) {
                return ['code' => 400, 'msg' => '用户已注册'];
            }
            if ($type == 'bind_mobile' && !empty($find)) {
                //小程序注册 无需处理 存在不存在都没事
            }
            if (($type == 'update_password' || $type == 'forget_password' || $type == 'set_pay_password') && empty($find)) {
                return ['code' => 400, 'msg' => '用户不存在'];
            }

            if ($type == 'update_username_old' && empty($find)) { //修改手机号的老手机号 必须存在
                return ['code' => 400, 'msg' => '用户不存在'];
            }
            if ($type == 'update_username' && !empty($find)) { //修改手机号的新手机号必须 不存在
                return ['code' => 400, 'msg' => '用户已注册'];
            }

            if ($type == 'invite' && !empty($find)) {
                return ['code' => 400, 'msg' => '用户已注册'];
            }
            $smsVerify = Cache::get('captcha_' . $type . '_expire_' . $mobile);
            if (!empty($smsVerify)) {
                 return ['code' => 401, 'message' => '60s内只能发送一次'];
            }
            $code = rand(100000, 999999);

            //$result = sendSMS($mobile, $code);
            $result = (new Sms())->sms($mobile,$code);
            if ($result) {
                Cache::set('captcha_' . $type . '_expire_' . $mobile, $code, 60); // 短信请求有效期
                writeLog('captcha_' . Cache::get('captcha2_' . $type . '_expire_' . $mobile), 'web_register_data');
                Cache::set('captcha_' . $type . '_' . $mobile, $code, 3600);     // 短信保存有效期
                return ['code' => 200, 'msg' => '发送成功', 'captcha' => $code];
            }
            return ['code' => 400, 'msg' => '失败'];
        } catch (\Exception $e) {
            return ['code' => 500, 'msg' => $e->getMessage()];
        }
    }

    public function index(Request $request)
    {

        try {
            $data = $this->model::withSearch(array_keys($request->data), $request->data)
                ->field($this->index)
//                ->withoutField($this->without_field)
                ->order($this->order)
                ->paginate($this->list_rows);
            if($this->append){
                $data->append($this->append);
            }
            if($this->hidden){
                $data->hidden($this->hidden);
            }
            return ['code' => 200, 'msg' => $this->msg . '列表', 'data' => $data];
        } catch (\Exception $e) {
            return ['code'=>$e->getCode(),'msg'=>$e->getMessage(),'line'=>$e->getTrace()];
        }
    }

    /*
 * 修改
 */
    public function update_bill(Request $request)
    {
        try {
            $info = $this->model::find($request->id);
            $ret = $info->save($request->data);
            if ($ret !== false) {
                return ['code' => 200, 'msg' =>'发票信息修改成功','data'=> $request->param];
            }
            return ['code' => 200, 'msg' => '失败'];
        } catch (\Exception $e) {
            return ['code' => $e->getCode(), 'msg' => $e->getMessage()];
        }
    }

    public function my_bill(Request $request){
        $data = $this->model::field(['id','bill_head','tax_id','address','bank','bank_account'])->find($request->id);
        $data['year_fee'] = \app\model\DeviceSigning::where('company_id',$request->id)->sum('price');
        return ['code'=>200,'msg'=>'我的发票','data'=>$data];
    }

    public function manage(Request $request)
    {

        try {
            $data = $this->model::withSearch(array_keys($request->data), $request->data)
                ->field(['id','name','login_account','login_password','remark','email','create_time','company_id'])
//                ->withoutField($this->without_field)
                ->order($this->order)
                ->paginate($this->list_rows)->each(function ($item,$key){
                    $item['company_name'] =  $this->model::where('id',$item['company_id'])->value('title');
                })->hidden(['company_id']);
            return ['code' => 200, 'msg' => '子管理员列表', 'data' => $data];
        } catch (\Exception $e) {
            return ['code'=>$e->getCode(),'msg'=>$e->getMessage(),'line'=>$e->getTrace()];
        }
    }

    public function rechange_records(Request $request)
    {

        writeLog(input());
        $data = CompanyBalance::withSearch(array_keys($request->data), $request->data)->paginate(10);

        return ['code' => 200, 'msg' => '充值记录', 'data' => $data];
    }






}
